package com.de.demo.client.service;

import com.de.demo.client.api.ZXingApi;
import com.de.demo.client.mapper.ZxingMapper;
import com.de.demo.client.pojo.Zxing;
import com.de.demo.client.utils.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class ZXingService implements ZXingApi {

    @Autowired
    private ZxingMapper zxingMapper;

    public String testZXing(){
        String url = "D:/pic/createPic/jam.jpg";
        Zxing selectOne = zxingMapper.selectOne(url);
        if(Objects.nonNull(selectOne)){
            log.info("二维码已经生成！！！");
            return selectOne.getUrl();
        }
        // 存放在二维码中的内容
        String text = "https://gitee.com/xiaoguoqiye_3289713531/cloud-demo";
        // 嵌入二维码的图片路径
        String imgPath = "D:/pic/1178811349.jpg";
        // 生成的二维码的路径及名称
        String destPath = "D:/pic/createPic/jam.jpg";
        try {
            //生成二维码
            QRCodeUtil.encode(text, imgPath, destPath, true);
        }catch (Exception e){
            log.info("扫描失败！！！");
        }
        Zxing zxing = new Zxing();
        zxing.setCreateTime(new Date());
        zxing.setName("cloud-demo码云地址二维码");
        zxing.setUrl(destPath);
        zxingMapper.insert(zxing);
      return destPath;
    }
}
